package com.bjpn.workbench.controller;


import com.bjpn.settings.bean.User;
import com.bjpn.util.DateToString;
import com.bjpn.util.MessageUtil;
import com.bjpn.util.ReturnObject;
import com.bjpn.workbench.bean.Activity;
import com.bjpn.workbench.bean.ActivityLike;
import com.bjpn.workbench.service.ActivityService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzy
 * @since 2022-09-06
 */
@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/toActivityIndex.action")
    public String toActivityIndex(){
        return "workbench/activity/index";
    }

    @RequestMapping("/saveActivity.action")
    @ResponseBody
    public ReturnObject saveActivity(Activity activity, ReturnObject robj, HttpSession session){
        String currentTime = DateToString.currentTimeToString();
        User user = (User)session.getAttribute("user");
        activity.setCreateBy(user.getUserId()+"");
        activity.setCreateTime(currentTime);
        boolean b = activityService.save(activity);
        if (b) {
            robj.setMessageCode(MessageUtil.SAVE_SUCCESS_CODE);
            robj.setMessageStr(MessageUtil.SAVE_SUCCESS_MESSAGE);
        }else {
            robj.setMessageCode(MessageUtil.SAVE_ERROR_CODE);
            robj.setMessageStr(MessageUtil.SAVE_ERROR_MESSAGE);
        }
        return robj;
    }

    // 分页查询带模糊  PageHelper
    @RequestMapping("/getActivityByPage.action")
    @ResponseBody
    public PageInfo<Activity> getActivityByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize,
                                                ActivityLike activityLike){
        System.out.println(activityLike);
        List<Activity> activityList = activityService.getActivityByPageService(pageNum, pageSize,activityLike);
        // 分页的产物PageInfo 分页信息 包括 查询到的数据集合都在里面
        // 紧邻且有先后顺序  navigatePages导航下标数
        return new PageInfo<>(activityList, 10);
    }

    // 批量删除
    @RequestMapping("/deleteActivityBatch.action")
    @ResponseBody
    public ReturnObject deleteActivityBatch(String idStr,ReturnObject robj){
        // 处理传过来的封装字符串
        String[] idArr = idStr.split(",");
        List<String> idList = Arrays.asList(idArr);
        boolean b = activityService.removeByIds(idList);
        if (b) {
            robj.setMessageCode(MessageUtil.DELETE_SUCCESS_CODE);
            robj.setMessageStr(MessageUtil.DELETE_SUCCESS_MESSAGE);
        }else {
            robj.setMessageCode(MessageUtil.DELETE_ERROR_CODE);
            robj.setMessageStr(MessageUtil.DELETE_ERROR_MESSAGE);
        }
        return robj;
    }

    // 查询数据 数据回显用
    @RequestMapping("/showActivity.action")
    @ResponseBody
    public Activity showActivity(Integer activityId){
        return activityService.getById(activityId);
    }

    // 修改
    @RequestMapping("/updateActivity.action")
    @ResponseBody
    public ReturnObject updateActivity(Activity activity,ReturnObject robj,HttpSession session){
        // 加入更新时间 和 更新人
        activity.setEditTime(DateToString.currentTimeToString());
        User user = (User) session.getAttribute("user");
        activity.setEditBy(user.getUserId() + "");
        boolean b = activityService.updateById(activity);
        if (b) {
            robj.setMessageCode(MessageUtil.UPDATE_SUCCESS_CODE);
            robj.setMessageStr(MessageUtil.UPDATE_SUCCESS_MESSAGE);
        }else {
            robj.setMessageCode(MessageUtil.UPDATE_ERROR_CODE);
            robj.setMessageStr(MessageUtil.UPDATE_ERROR_MESSAGE);
        }
        return robj;
    }

    // 导出活动excel
    @RequestMapping("/exportAllActivity.action")
    public void exportAllActivity(HttpServletResponse response) throws IOException {
        List<Activity> activityList = activityService.getAllActivity();
        // 创建poi
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("市场活动列表");
        HSSFRow row = sheet.createRow(0);   // 行
        HSSFCell cell = row.createCell(0);  // 单元格
        cell.setCellValue("ID");
        // 重复利用cell 更节省空间
        cell=row.createCell(1);
        cell.setCellValue("所有者");
        cell=row.createCell(2);
        cell.setCellValue("名称");
        cell=row.createCell(3);
        cell.setCellValue("开始日期");
        cell=row.createCell(4);
        cell.setCellValue("结束日期");
        cell=row.createCell(5);
        cell.setCellValue("成本");
        cell=row.createCell(6);
        cell.setCellValue("描述");
        cell=row.createCell(7);
        cell.setCellValue("创建时间");
        cell=row.createCell(8);
        cell.setCellValue("创建者");
        cell=row.createCell(9);
        cell.setCellValue("修改时间");
        cell=row.createCell(10);
        cell.setCellValue("修改者");
        if (activityList != null && activityList.size() > 0) {
            Activity activity = null;
            // 另起行
            for (int i = 0; i < activityList.size(); i++) {
                activity = activityList.get(i);
                row = sheet.createRow(i + 1);
                cell=row.createCell(0);
                cell.setCellValue(activity.getActivityId());
                cell=row.createCell(1);
                cell.setCellValue(activity.getActivityOwner());
                cell=row.createCell(2);
                cell.setCellValue(activity.getActivityName());
                cell=row.createCell(3);
                cell.setCellValue(activity.getActivityStartDate());
                cell=row.createCell(4);
                cell.setCellValue(activity.getActivityEndDate());
                cell=row.createCell(5);
                cell.setCellValue(activity.getActivityCost());
                cell=row.createCell(6);
                cell.setCellValue(activity.getActivityDescription());
                cell=row.createCell(7);
                cell.setCellValue(activity.getCreateTime());
                cell=row.createCell(8);
                cell.setCellValue(activity.getCreateBy());
                cell=row.createCell(9);
                cell.setCellValue(activity.getEditTime());
                cell=row.createCell(10);
                cell.setCellValue(activity.getEditBy());
            }
        }
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.addHeader("Content-Disposition","attachment;filename=activityList.xls");
        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        wb.close();
        out.flush();
    }

}

